<template>
    <div class="app-container">
        <!-- 添加字典类型对象 -->
        <el-form ref="form" :model="dictTypeForm" :rules="rules"  label-width="80px">
            <el-form-item label="编号" prop="code">
                <el-input v-model="dictTypeForm.code" :disabled="dictTypeForm.id != null ? true : false" placeholder="请输入编号" />
            </el-form-item>
            <el-form-item label="字典名称" prop="name">
                <el-input v-model="dictTypeForm.name" placeholder="请输入字典名称" />
            </el-form-item>
            <el-form-item label="英文名" prop="enName">
                <el-input v-model="dictTypeForm.enName" placeholder="请输入英文名" />
            </el-form-item>
        </el-form>
        <div class="dialog-footer" style="float:right">
            <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </div>
</template>

<script>
    import { addLevelTwoType, changeLevelTwoType} from '@/api/basicdata/basictype'
    export default {
        name: "dictTypeForm",
        props: {
            // 字典类型信息
            dictTypeForm: Object,
        },
        data() {
            var checkCode = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('编号不能为空'));
                }/* else if (!/^\w{1}\d{5}$/.test(value)) {
                    return callback(new Error('必须符合编码规则！'));
                }*/ else {
                    callback();
                }
            };
            return {
                // 按钮loading
                buttonLoading: false,
                // 表单校验
                rules: {
                    name: [
                        { required: true, message: "字典名称不能为空", trigger: "blur" }
                    ],
                    code: [
                        { required: true,validator: checkCode, trigger: "blur" }
                    ]
                },


            }
        },
        methods: {
            // 添加/修改字典类型
            submitForm() {
                // console.log('dictTypeForm===:', JSON.stringify(this.dictTypeForm))
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        this.buttonLoading = true;
                        if (this.dictTypeForm.id != null) {
                            changeLevelTwoType(this.dictTypeForm).then(res => {
                                // console.log(res)
                                if (res.retCode === '200') {
                                    this.$message.success('修改成功')
                                    this.cancel();
                                    // 重新加载
                                    this.$emit("submitForm");
                                } else {
                                    // console.log(res)
                                    this.$message.error('修改失败' + res.retCode)
                                }
                            }).finally(() => {
                                this.buttonLoading = false;
                            });
                        } else {
                            addLevelTwoType(this.dictTypeForm).then(res => {
                                // console.log(res)
                                if (res.retCode === '200') {
                                    this.$message.success('添加成功')
                                    this.cancel();
                                    // 重新加载
                                    this.$emit("submitForm");
                                } else {
                                    this.$message.error('请求失败' + res.retCode)
                                }
                            }).finally(() => {
                                this.buttonLoading = false;
                            });
                        }
                    }
                });
            },
            // 取消按钮
            cancel() {
                // 清空
                this.$emit("cancelDialog");
            },
        }

    }

</script>
